﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using Neo4jConnect.Rest.Extensions;

namespace Neo4jConnect.Rest
{
    public class HttpClientRestService : IRestService
    {
        private HttpClient client;

        public string BaseUri
        {
            get { return client.BaseAddress.ToString(); }
        }

        public INodeService Node { get; private set; }
        public IRelationshipService Relationship { get; private set; }
        public IIndexService Index { get; private set; }
        public ITraversalService Traversal { get; private set; }

        public IExtensionService Extension { get; private set; }
        public IExtensionManager ExtensionManager { get; private set; }

        public HttpClientRestService(string baseUri) : this(new Uri(baseUri))
        {
        }

        public HttpClientRestService(Uri baseUri) : this(baseUri, int.MaxValue)
        {
        }

        public HttpClientRestService(Uri baseUri, int maxResponseContentBufferSize)
        {
            client = new HttpClient();
            client.BaseAddress = baseUri;
            client.MaxResponseContentBufferSize = int.MaxValue;
            client.MaxResponseContentBufferSize = maxResponseContentBufferSize;
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //client.Channel = new MyChannel(new HttpClientChannel());
                                                           
            this.Node = new HttpClientNodeService(client);
            this.Relationship = new HttpClientRelationshipService(client);
            this.Index = new HttpClientIndexService(client);
            this.Traversal = new HttpClientTraversalService(client);
            this.Extension = new HttpClientExtensionService(client);
            this.ExtensionManager = new ExtensionManager(client);
        }
    }

    //public class MyChannel : DelegatingChannel
    //{
    //    public MyChannel(HttpMessageChannel innerChannel) : base(innerChannel) { }

    //    protected override HttpResponseMessage Send(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    //    {
    //        var response = base.Send(request, cancellationToken);
    //        return response;
    //    }
    //}
}
